package org.td.engine.entity;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import org.td.engine.map.GameMap;
import org.td.engine.Path;
import org.td.engine.Path.Node;

public class Monster extends Entity {
    
    private Path path;
    
    private int currentNodeIndex;
    private Node currentNode;
    private Node nextNode;

    private MonsterClass monsterClass;
    
    private float health;
    private HealthBar healthBar;
    
    public Monster(EntityEnvironment environment, MonsterClass monsterClass, Path path) {
        super(environment, monsterClass);
        this.monsterClass = monsterClass;
        currentNodeIndex = 0;
        currentNode = path.getStart();
        nextNode = currentNode;
        this.path = path;
        
        health = monsterClass.getMaxHealth();
        healthBar = new HealthBar(40);

        setPosition(environment.indexedToX(currentNode.x), environment.indexedToY(currentNode.y));
    }

    @Override
    public void update(float time) {
        boolean canMove = true;
        if (nextNode == currentNode) {
            canMove = getNextNode();
        } else {

        }
        
        if (canMove) {
            float absolX = environment.indexedToX(nextNode.x) + environment.getCellWidth() / 2f;
            float absolY = environment.indexedToY(nextNode.y) + environment.getCellHeight() / 2f;
            
            Vector2 radVec = new Vector2(absolX - x, absolY - y);
            Vector2 dirVec = radVec.nor();
            
            float speed = monsterClass.getSpeed() * time;
            
            double dist = Math.sqrt((x-absolX)*(x-absolX) + (y-absolY)*(y-absolY));
            boolean reached = false;
            if (dist <= speed) {
                speed = (float)dist;
                reached = true;
            }
            
            x += dirVec.x * speed;
            y += dirVec.y * speed;
            
            if (reached) {
                currentNode = nextNode;
            }
        }

        if (health <= 0f) {
            clear = true;
        } else {
            healthBar.update(x, y + 16, health / monsterClass.getMaxHealth());
        }
    }

    @Override
    public void render(SpriteBatch batch) {
        super.render(batch);
        healthBar.render(batch);
    }
    
    public float getHealth() {
        return health;
    }
    
    public void setHealth(float health) {
        this.health = health;
    }
    
    public void addHealth(float amount) {
        this.health += amount;
    }
    
    private boolean getNextNode() {
        if (currentNodeIndex < path.getNodeCount()-1) {
            nextNode = path.getNext(currentNodeIndex++);
            return true;
        } else {
            return false;
        }
    }

    public float getSize() {
        return monsterClass.getSize();
    }
    
    @Override
    public EntityClass getEntityClass() {
        return monsterClass;
    }

}
